package splash;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import coganh.Activity.R;

public class SplashThread extends Thread {
	public static final int STATE_LOSE = 1;
	public static final int STATE_PAUSE = 2;
	public static final int STATE_READY = 3;
	public static final int STATE_RUNNING = 4;
	public static final int STATE_WIN = 5;

	private static final String KEY_DIFFICULTY = "mDifficulty";
	private static final String KEY_WINS = "mWinsInARow";

	private Bitmap b;

	private ArrayList<SplashSprite> mSprites;
	private ArrayList<SplashSprite> mSpritestoRemove;

	private int mDifficulty;

	private SplashView mAnimationView;

	private int mMode;

	private long mLastTime;

	private boolean mRun = false;

	private int frameSamplesCollected = 0;
	private int frameSampleTime = 0;
	private int fps = 0;

	private SurfaceHolder mSurfaceHolder;

	private Paint textPaint;

	private int numSprites;

	private Resources res;

	public SplashThread(SurfaceHolder surfaceHolder, Context context) {
		// get handles to some important objects
		mSurfaceHolder = surfaceHolder;
		res = context.getResources();

		mSprites = new ArrayList<SplashSprite>();
		mSpritestoRemove = new ArrayList<SplashSprite>();

		textPaint = new Paint();
		textPaint.setARGB(255, 255, 255, 255);
		textPaint.setTextSize(16);

		numSprites = 0;
	}

	@Override
	public void run() {
		while (mRun) {
			Canvas c = null;
			try {
				c = mSurfaceHolder.lockCanvas(null);
				synchronized (mSurfaceHolder) {
					if (mMode == STATE_RUNNING)
						updatePhysics();
					doDraw(c);
				}
			} catch (Exception e) {
				System.out.println(e.getStackTrace());
			} finally {
				if (c != null) {
					mSurfaceHolder.unlockCanvasAndPost(c);
				}
			}
		}
	}

	/**
	 * Draws to the provided Canvas.
	 */
	private void doDraw(Canvas canvas) {
		canvas.drawColor(Color.BLACK);

		for (SplashSprite a : mSprites) {
			a.draw(canvas);
		}

		canvas.restore();
	}

	private void updatePhysics() {
		long now = System.currentTimeMillis();

		if (mLastTime > now)
			return;

		if (mLastTime != 0) {
			int time = (int) (now - mLastTime);
			frameSampleTime += time;
			frameSamplesCollected++;
			if (frameSamplesCollected == 10) {
				fps = (int) (10000 / frameSampleTime);
				frameSampleTime = 0;
				frameSamplesCollected = 0;
			}
		}

		synchronized (mSprites) {
			for (SplashSprite a : mSprites) {
				a.Update(now);

				if (a.dispose)
					mSpritestoRemove.add(a);
			}
		}

		synchronized (mSpritestoRemove) {
			mSprites.removeAll(mSpritestoRemove);
			mSpritestoRemove.clear();
		}

		numSprites = mSprites.size();
		mLastTime = now;

	}

	public void doTouch(MotionEvent event) {

		int action = event.getAction();
		float x = event.getX(); // or getRawX();
		float y = event.getY();

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			if (mMode != STATE_RUNNING)
				setState(STATE_RUNNING);
			else if (mMode == STATE_RUNNING) {

				SplashSprite a = new SplashSprite();

				if (b == null)
					b = BitmapFactory.decodeResource(res, R.drawable.explosion);

				a.Initialize(b, 120, 160, 24, 20, true);

				a.setxPost((int) x);
				a.setyPost((int) y);

				synchronized (mSprites) {
					mSprites.add(a);
				}
			}
			break;
		}
	}

	public void setState(int mode, CharSequence message) {
		synchronized (mSurfaceHolder) {
			mMode = mode;

			if (mMode == STATE_RUNNING)
				;
			else {
				if (mMode == STATE_READY)
					;
				else if (mMode == STATE_PAUSE)
					;
				else if (mMode == STATE_LOSE)
					;
				else if (mMode == STATE_WIN)
					;
				if (mMode == STATE_LOSE)
					numSprites = 0;

			}
		}
	}

	public Bundle saveState(Bundle map) {
		synchronized (mSurfaceHolder) {
			if (map != null) {
				map.putInt(KEY_DIFFICULTY, Integer.valueOf(mDifficulty));
				map.putInt(KEY_WINS, Integer.valueOf(numSprites));
			}
		}
		return map;
	}

	public void setDifficulty(int difficulty) {
		synchronized (mSurfaceHolder) {
			mDifficulty = difficulty;
		}
	}

	public void setRunning(boolean b) {
		mRun = b;
	}

	public void setState(int mode) {
		synchronized (mSurfaceHolder) {
			setState(mode, null);
		}
	}

	public void setSurfaceSize(int width, int height) {
		synchronized (mSurfaceHolder) {

		}
	}

	public void unpause() {
		synchronized (mSurfaceHolder) {
			mLastTime = System.currentTimeMillis() + 100;
		}
		setState(STATE_RUNNING);
	}

	public void doStart() {
		synchronized (mSurfaceHolder) {
			mLastTime = System.currentTimeMillis() + 100;
			setState(STATE_RUNNING);
		}
	}

	public void pause() {
		synchronized (mSurfaceHolder) {
			if (mMode == STATE_RUNNING)
				setState(STATE_PAUSE);
		}
	}

	public synchronized void restoreState(Bundle savedState) {
		synchronized (mSurfaceHolder) {
			setState(STATE_PAUSE);

			mDifficulty = savedState.getInt(KEY_DIFFICULTY);
			numSprites = savedState.getInt(KEY_WINS);
		}
	}

	public void clearSprites() {
		synchronized (mSprites) {
			mSprites.clear();
		}
	}
}
